<?php
	class TeamsController extends AppController{
		
		var $components = array( 'Email' );
		var $uses = array( 'Team', 'Invitation', 'PeopleTeams', 'User' );
		
		function index(){
	
			$listTeams = $this->Team->findAllByPersonId(1);
			
			$this->set( compact( 'listTeams' ) );
		}
		
		
		function view( $intTeamId ){
			$this->Team->id = $intTeamId;
			$listTeam = $this->Team->read();
		
			$this->set( compact( 'listTeam' ) );
		}
		
		function add(){
			$this->data['Team']['person_id'] = "1";
			if(empty($this->data['Team']['name']) || empty($this->data['Team']['description'])){
					$this->Session->setFlash("El equipo ha sido salvado con exito.");	
					return false;
			}else{
				$this->Team->save($this->data);
				$this->Session->setFlash("El equipo ha sido salvado con exito.");
			}
					
			$this->redirect( 'index' );
		}
		
		function edit($intTeamId){
			if(!empty($this->data)){
				if($this->Team->save($this->data)){
					$this->Session->setFlash("Equipo Modificado!");
					$this->redirect('index');
				}
							
			}
			$this->data = $this->Team->findById($intTeamId);
		}
		
		function delete($intTeamId){
			$this->Team->id = $intTeamId;
			$this->Team->remove();
			$this->Session->setFlash( 'El equipo ha sido eliminado con exito.' );
			$this->redirect('index');
		}
		
		function invite(){
			$this->Email->sendAs = 'html';
			$this->Email->to = $this->data['Person']['email'];
			$this->Email->from = 'hacakthon@itla.edu.do';
			$this->Email->subject = 'Invitacion a Grupo';
			$this->Email->template = 'invitation';
			
			$listInvitation = array(
				'email' => $this->data['Person']['email'],
				'person_id' => 1,
				'team_id' => $this->data['Team']['id'],
				'is_accepted' => false	
			);
			
			if( $this->Invitation->save( $listInvitation ) ){
					$this->set( 'strPersonName', 'Ruben Jimenez' );
					$this->set( 'strPersonName', $this->data['Team']['name'] );
					$this->set( 'strMessage', $this->data['Invitation']['message'] );
					$this->set( 'intInvitationId', $this->Invitation->getInsertID() );
					
					$this->Session->setFlash( 'La persona ha sido invitada. Deberas esperar hasta que acepte la invitacion.' );
					$this->Email->send();
			}else{
				$this->Session->setFlash( 'Ha ocurrido un error al tratar de crear la invitacion. Por favor intentelo mas tarde.' );
			}
			
			$this->redirect( $this->referer() );
		}
		
		function removePerson( $intTeamId, $intPersonId ){
			$this->PeopleTeams->deleteAll( array( 'team_id' => $intTeamId, 'person_id' => $intPersonId ) );
			$this->Session->setFlash( 'El miembro ha sido eliminado de este grupo' );
			$this->redirect( $this->referer );
		}
		
		function acceptInvite( $intInvitationId ){
			$this->Invitation->id = $intInvitationId;
			$listInvitation = $this->Invitation->read();
			
			if( $this->Session->read('invitation_id') ){
				$this->Session->delete( 'invitation_id' );
				$listUser = $this->Session->read( 'Auth.User' );
			}else{
				$listUser = $this->User->findByEmail(  $listInvitation['Invitation']['email'] );
			}
			
			$this->Invitation->begin();

			if( $listUser ){
				$listPeopleTeams = array(
					'person_id' => $listUser['Person']['id'],
					'team_id' => $listInvitation['Invitation']['team_id']
				); 
				
				if( $this->PeopleTeams->save( $listPeopleTeams ) ){
					$this->Invitation->saveField( 'is_accepted', true );
					$this->Invitation->commit();
					$this->Session->setFlash( 'Acabas de ser agregado a este equipo.' );
					$this->redirect( "view/{$listInvitation['Invitation']['team_id']}" );
				}else{
					$this->Session->setFlash( 'Hubo un error procesando la solicitud, por favor intentelo mas tarde.' );
					$this->Invitation->rollback();
					$this->redirect( '/' );
				}
			} else{
				$this->Session->write( 'invitation_id', $intInvitationId );
				$this->redirect( '/users/login' );
			}
		}
	}
	
?>